##
## BWC Meta-analysis
## R Code to Read Data from MySQL server
## and run analyses
## Created by D.B.Wilson
## Lasted Edited June 5, 2020
##

##------------------------------------------------------------------
##
## Load libraries
##
##------------------------------------------------------------------
library("tidyverse")
library("dplyr")
library("knitr")
library("kableExtra")
library("metafor")
library("janitor")
library("data.table")
library("tools")
library("stringr")
library("lubridate")
library("svglite")
library("ggplot2")
library("ggthemes")
options(width=200,scipen=999)

##------------------------------------------------------------------
##
## Read CSV files
##
##------------------------------------------------------------------

setwd("C:/Users/awjor/Dropbox/misconduct/Replication")

data<- read.csv("data/google trends.csv", header=TRUE, stringsAsFactors=FALSE)
 
# Convert Month to date format
data$Month <- as.Date(paste(data$Month, "01", sep = "-"))

# Plot the data
p <- ggplot(data, aes(x = Month)) +
  geom_line(aes(y = black.lives.matter, color = "Black Lives Matter")) +
  geom_line(aes(y = police.brutality, color = "Police Brutality")) +
  geom_vline(aes(xintercept = as.Date("2014-08-01"), linetype = "Michael Brown")) +
  geom_vline(aes(xintercept = as.Date("2015-11-01"), linetype = "Laquan McDonald Video")) +
  labs(x = "Time", y = "Google Search Interest Index", color = "Topic", linetype = "Event") +
  scale_color_manual(values = c("Black Lives Matter" = "blue", "Police Brutality" = "dark green")) +
  scale_linetype_manual(values = c("Michael Brown" = "dotted", "Laquan McDonald Video" = "longdash")) +
  guides(color = guide_legend(reverse = TRUE), linetype = guide_legend(reverse = TRUE)) +
  theme_minimal() +
  scale_x_date(date_labels = "%Y", date_breaks = "2 year", expand = c(0, 0), limits = as.Date(c("2006-01-01", "2020-12-31"))) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "bottom",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line(color = "black"))

# Save the plot to a PDF file
ggsave("output/figures/google_trends.pdf", plot = p, width = 8, height = 6)

 
# Read the CSV file
 
protest_data<- read.csv("data/blm_data_cleaned.csv", header=TRUE, stringsAsFactors=FALSE)

# Clean the yearmo column
protest_data$year <- substr(protest_data$yearmo, 1, 4)
protest_data$month <- as.numeric(gsub("^\\d{4}m(\\d+)$", "\\1", protest_data$yearmo))
# Plot the data
p2 <-ggplot(protest_data, aes(x = as.Date(paste0(year, "-", month, "-01")), y = obs)) +
  geom_line(color = "blue") +
  geom_vline(aes(xintercept = as.Date("2015-11-01"), linetype = "Laquan McDonald Video")) +
  scale_linetype_manual(values = c("Laquan McDonald Video" = "longdash")) +
  labs(x = "Time", y = "Monthly Total Number of Protests in Chicago") +
  theme_minimal() +
  scale_x_date(date_labels = "%Y", date_breaks = "2 year", expand = c(0, 0), limits = as.Date(c("2006-01-01", "2020-12-31"))) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "none",
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line(color = "black"))

ggsave("output/figures/protest_trends.pdf", plot = p2, width = 8, height = 6)

